<?php

namespace app\backend\controller;

use app\common\model\BgPosts;
use think\Controller;
use think\Request;

class Post extends Controller
{
    //文章列表
    public function index()
    {
        //获得文章数据
        $posts=BgPosts::useGlobalScope(false)->order('id','desc')->paginate(5);

        $this->assign(compact('posts'));
        return $this->fetch();
    }

    //待审核
    public function unaudited()
    {
        $posts=BgPosts::useGlobalScope(false)->where('status',0)->order('id','desc')->paginate(5);

        $this->assign(compact('posts'));
        return $this->fetch();
    }

    //未通过
    public function nopass()
    {
        $posts=BgPosts::useGlobalScope(false)->where('status',2)->order('id','desc')->paginate(5);

        $this->assign(compact('posts'));
        return $this->fetch();
    }

    //通过页面
    public function pass()
    {
        $posts=$posts=BgPosts::order('id','desc')->paginate(5);

        $this->assign(compact('posts'));
        return $this->fetch();
    }

    //操作
    public function opt(Request $request)
    {
        $id=$request->post('id',0,'trim,strip_tags,intval');
        $opt=$request->post('opt','','trim,strip_tags');

        //查找id对应的文章信息
        $post_info=BgPosts::useGlobalScope(false)->where('id',$id)->find();

        switch($opt){
            //送审
            case 'review':
                $post_info->status=0;
                break;
            //通过
            case 'pass':
                $post_info->status=1;
                break;
            case 'refuse':
                $post_info->status=2;
                break;
            default:
                return json(['msg'=>'非法操作','code'=>-1]);
                break;
        }

        $post_info->save();

        return json(['msg'=>'操作成功','code'=>200]);
    }
}
